% 读取原始图像
image = imread('pic_original.png');
gray_image =uint8(image); % 转换为灰度图像
%left=235.*ones(h,50,3);
added=gray_image;
%added=[left,gray_image];
added=rgb2hsv(added);
added=255.*rgb2gray(added);
imwrite(added,"added.png");
gray_image=added;


adaptive_thresh = adaptthresh(gray_image, 0.5); % 参数 0.5 控制灵敏度
binary_image = imbinarize(gray_image, adaptive_thresh);
imwrite(binary_image, 'adaptive_binary.png');

% 使用高斯滤波分离阴影
low_pass = imgaussfilt(gray_image, 50); % 50 表示滤波半径，调节可控
shadow_removed = imsubtract(gray_image, low_pass); % 原图减去阴影
imwrite(shadow_removed, 'shadow_removed.png');


% 背景建模
background = imgaussfilt(gray_image, 20); % 模糊作为背景
foreground = imsubtract(gray_image, background); % 减去背景
imwrite(foreground, 'foreground_without_shadow.png');

% 设定亮度阈值
threshold = 50; % 灰度值阈值
shadow_mask = gray_image > threshold;

% 去除阴影
shadow_removed = uint8(gray_image) .* uint8(shadow_mask);
imwrite(shadow_removed, 'filtered_shadow.png');
